package antlr.caculator.app;

import antlr.caculator.gramma.caculatorLexer;
import antlr.caculator.gramma.caculatorParser;
import org.antlr.v4.runtime.CharStream;
import org.antlr.v4.runtime.CharStreams;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.tree.ParseTree;


import java.io.*;

public class CaculatorMain {
    public static void main(String[] args) {
        long start = System.currentTimeMillis();
        //new ByteArrayInputStream("int a=3*7+9/3-5*8;int b = 8-7;".getBytes());
        CharStream inputStream = null;
        try {
            InputStream stream = CaculatorMain.class.getResourceAsStream("/multi-math.cld");
            inputStream = CharStreams.fromStream(stream);
        } catch (IOException e) {
            e.printStackTrace();
        }
        caculatorLexer lexer = new caculatorLexer(inputStream);

        CommonTokenStream tokenStream = new CommonTokenStream(lexer);
        caculatorParser parser = new caculatorParser(tokenStream);
        ParseTree exprTree = parser.stat();
        CaculatorVistorImpl visitor = new CaculatorVistorImpl();
        Object expr = visitor.visit(exprTree);
        System.out.println("#expr#"+expr);
        System.out.println(System.currentTimeMillis()-start);
    }
}
